package com.zuipin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.MemberRecord;
import com.zuipin.enums.MemberRecordType;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.service.IMemberRecordService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.Pagination;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/** 
 * @ClassName: MemberRecordServiceImpl 
 * @author wuyicheng
 * @time 2017年3月27日下午4:02:46 
 */
@Api(value = "会员日志", description = "会员日志-wuyicheng")
@RestController
@RequestMapping(value = "/member/memberRecord")
public class MemberRecordController {
	@Resource
	private IMemberRecordService	memberRecordService;
	@Resource
	private IMemberAccountService	memberAccountService;
	
	@RequestMapping(value = "/createSignInLog", method = RequestMethod.POST)
	@ApiOperation(value = "会员签到-wuyicheng", notes = "会员签到-wuyicheng")
	public Result<Map<String, Object>> createSignInLog(
			@ApiParam(required = true, name = "memberId", value = "会员id") @RequestParam Long memberId,
			@ApiParam(required = true, name = "sysId", value = "系统id") @RequestParam Long sysId)
			throws CustomException {
		
		Result<Map<String, Object>> result = new Result<>();
		result = memberRecordService.doSignInToday(memberId, sysId, MemberRecordType.SIGN_IN,
				"会员已经签到", result);
		return result;
	}
	
	@RequestMapping(value = "/searchIsSignIn", method = RequestMethod.GET)
	@ApiOperation(value = "是否已经签到-wuyicheng", notes = "是否已经签到-wuyicheng")
	public Result<Map<String, Object>> searchIsSignIn(
			@ApiParam(required = true, name = "memberId", value = "会员id") @RequestParam Long memberId,
			@ApiParam(required = true, name = "sysId", value = "系统id") @RequestParam Long sysId) {
		
		Result<Map<String, Object>> result = new Result<>();
		Map<String, Object> map = new HashMap<>();
		boolean isExist = memberAccountService.doIsExist(memberId, sysId);
		if (isExist) {
			boolean flag = memberRecordService.isSignInToday(memberId, sysId);
			if (flag) {
				map.put(ConstantsUtil.RETURN_RESULT, true);
				result.setErrorCode(ErrorCode.SUCCESS);
			} else {
				map.put(ConstantsUtil.RETURN_RESULT, false);
				result.setErrorCode(ErrorCode.SUCCESS);
			}
		} else {
			map.put(ConstantsUtil.RETURN_RESULT, false);
			result.setErrorCode(ErrorCode.OBJECT_EXIST);
			result.setMessage("memberAccount is not exist");
		}
		result.setData(map);
		return result;
	}
	
	@RequestMapping(value = "/searchSignInRecord", method = RequestMethod.GET)
	@ApiOperation(value = "查询会员签到记录-wuyicheng", notes = "查询会员签到记录-wuyicheng")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "pageNumber", value = "页码", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页面大小", required = false, dataType = "integer", paramType = "query") })
	public Result<List<MemberRecord>> searchSignInRecord(@RequestParam Long memberId,
			@RequestParam Long sysId, @RequestParam(required = false) Integer pageSize,
			@RequestParam(required = false) Integer pageNumber) {
		PageResult<List<MemberRecord>> result = new PageResult<>();
		Pagination pagination = new Pagination();
		if (pageNumber == null) {
			pageNumber = Pagination.DEFAULT_PAGE_INDEX;
		}
		if (pageSize == null) {
			pageSize = Pagination.DEFAULT_PAGE_SIZE;
		}
		pagination.setPageNumber(pageNumber);
		pagination.setPageSize(pageSize);
		List<MemberRecord> memberRecords = memberRecordService.searchSignInByPage(memberId, sysId,
				pagination);
		if (memberRecords != null && memberRecords.size() > 0) {
			result.setTotal_record(memberRecords.size());
		} else {
			result.setTotal_record(0);
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(memberRecords);
		return result;
	}
}
